import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sptapp/api/interface/user_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/config/const.dart';
import 'package:sptapp/moudle/common_info.dart';
import 'package:sptapp/widget/common_widget.dart';
import 'package:url_launcher/url_launcher.dart';

class MinAboutPage extends StatefulWidget {
  const MinAboutPage({super.key});

  @override
  State<MinAboutPage> createState() => _MinAboutPageState();
}

class _MinAboutPageState extends State<MinAboutPage> {

  bool isUpdate = false;
  String? version;
  
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getVersion();
  }

  _getVersion()async{
    ResponseData res = await UserAPI.getVersion({
      'type':Platform.isIOS?'ios':'android'
    });
    if(res.code == 200 && res.data != null){
      version = res.data;
      double num = double.parse(version!.split('.').join(""));
      double bv = double.parse(baseVersionNum.split('.').join(""));
      if(num > bv){
        isUpdate = true;
      }
    }
    if(mounted){
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: defaultBlackBgColor,
      appBar: myAppBar(context, 'Version'),
      body: Container(
        width: 1.sw,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            SizedBox(height: 260.w,),
            Image.asset('assets/home/min_about.png',width: 160.w,),
            SizedBox(height: 32.w,),
            Container(
              child: Text('SPT',style: TextStyle(
                color: Colors.white,
                fontSize: 32.sp,
                fontWeight: FontWeight.w400
              ),),
            ),
            SizedBox(height: 32.w,),
            Container(
              child: Text('The current version is $baseVersion',style: TextStyle(
                  color: Color.fromRGBO(255, 255, 255, 0.5),
                  fontSize: 32.sp,
                  fontWeight: FontWeight.w400
              ),),
            ),
            Visibility(child: Container(
              width: 670.w,
              child: Text('You can upgrade to the lastest version to access more features',style: TextStyle(
                  color: Color.fromRGBO(255, 255, 255, 0.5),
                  fontSize: 32.sp,
                  fontWeight: FontWeight.w400
              ),textAlign: TextAlign.center,),
            ),visible: isUpdate,),
            SizedBox(height: 64.w,),
            Visibility(child: GestureDetector(
              onTap: ()async{
                if(Platform.isAndroid){
                  launchUrl(Uri.parse('https://play.google.com/store/apps/details?id=com.stored.power.sptapp'),mode: LaunchMode.externalApplication);
                }else{
                  launchUrl(Uri.parse('https://apps.apple.com/cn/app/ispt/id6702013285'),mode: LaunchMode.externalApplication);
                }
              },
              child: Container(
                height: 108.w,
                width: 670.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child: Text('Update'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 32.sp,
                    fontWeight: FontWeight.w600
                )),
              ),
            ),visible: isUpdate,)
          ],
        ),
      ),
    );
  }
}
